package com.guigu.www.denghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHBespeakResultService;
import com.guigu.www.mapper.BespeakMapper;
import com.guigu.www.mapper.BespeakResultMapper;
import com.guigu.www.mapper.PersonClassificationMapper;
import com.guigu.www.pojo.Bespeak;
import com.guigu.www.pojo.BespeakResult;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.pojo.PersonClassification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DHBespeakResultServiceImpl implements DHBespeakResultService {

    @Autowired
    private BespeakResultMapper resultMapper;
    @Autowired
    private PersonClassificationMapper classificationMapper;
    @Autowired
    private BespeakMapper bespeakMapper;

    @Override
    public List<PersonClassification> queryClassificationAll() {
        return classificationMapper.selectList(null);
    }

    @Override
    public void submitUpload(BespeakResult bespeakResult) {
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );

        bespeakResult.setBesResultCreateTime(time);
        bespeakResult.setPerClassId(bespeakResult.getBesResultNormal());
        bespeakResult.setBesResultConfirm(0);
        resultMapper.insert(bespeakResult);
        Bespeak bespeak = new Bespeak();
        bespeak.setBesId(bespeakResult.getBesId());
        bespeak.setBesComplete(1);
        bespeak.setBesIsUpload(1);
        bespeakMapper.updateById(bespeak);
    }

    @Override
    public BespeakResult showUpload(Integer besId) {
        return resultMapper.selectOne(new QueryWrapper<BespeakResult>().eq("bes_id",besId));
    }

    //分页查询预约列表
    @Override
    public PageVo<Bespeak> selldespeakall(Map<String, String> map) {
        QueryWrapper<Bespeak> wrapper = new QueryWrapper<>();
        wrapper.like("bes_id", map.get("findid")).like("bes_name", map.get("findname")).like("bes_phone", map.get("findphone"));
        wrapper.eq("bes_state",1);
        wrapper.orderByDesc("bes_time");
        IPage<Bespeak> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = bespeakMapper.selectPage(page, wrapper);
        PageVo<Bespeak> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        return pageVo;
    }

}
